﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Core;

namespace Enough.Lifecycle.Helper
{
    public static class UiThreadHelper
    {
        private static CoreDispatcher _coreDispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher;

        public static bool IsOnUiThread()
        {
            return _coreDispatcher.HasThreadAccess;
        }

        public static async Task RunOnUiThread(DispatchedHandler handler)
        {
            if (IsOnUiThread())
            {
                handler.Invoke();
            }
            else
            {
                await _coreDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, handler);
            }
        }
    }
}
